package com.huishou.lajiMain.controller;

import com.huishou.lajiMain.config.OssConfig;
import com.huishou.lajiMain.config.result.Result;
import com.huishou.lajiMain.config.zhujie.LogginInfo;
import com.huishou.lajiMain.config.zhujie.LoginStr;
import com.huishou.lajiMain.entity.DTO.FileUploadDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;


/*
 * @Author:大力王
 * @Date:2024/5/11-11-2:05
 * 开始了
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件接口上传或下载")
public class FilesController {
    @Resource
    private OssConfig ossConfig;

//    上传文件接口
    @ApiOperation(value = "上传文件接口")
    @PostMapping("/upload")
    @LogginInfo
    @LoginStr(value = "你好！我是打印注解")
    public Result<FileUploadDTO> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("文件类型")String fileType) throws IOException {
        //数据库保存里面
        String path = ossConfig.uploadFile(file, fileType);
        //文件回显路径
        String url = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+path;
        return Result.success(new FileUploadDTO(url,path));
    }


    //下载文件接口
    @ApiOperation(value = "通用下载文件的接口")
    @PostMapping("/download")
    public ResponseEntity<ByteArrayResource> downloadFile(@RequestParam("objectName")String objectName) throws Exception {
        ByteArrayOutputStream outputStream = ossConfig.getObject(objectName);
        //定义一个相应头
        HttpHeaders httpHeaders = new HttpHeaders();

        //获取文件后缀
        String suffix = objectName.substring(objectName.lastIndexOf("."));
        //获取时间戳
        String time = String.valueOf(System.currentTimeMillis());
        objectName = "yy下载站_"+time+suffix;
        System.out.println("objectName = " + objectName);
        //设置下载文件名
        //中文名不乱码
        httpHeaders.setContentDispositionFormData("yy下载站_"+time, URLEncoder.encode(objectName, "utf-8"));
        httpHeaders.add("Cache-Control", "no-cache, no-store, must-revalidate");
        httpHeaders.add("Pragma", "no-cache");
        httpHeaders.add("Expires", "0");

        //设置相应内容是二进制流
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ByteArrayResource byteArrayResource = new ByteArrayResource(outputStream.toByteArray());

        return ResponseEntity.ok()
                .headers(httpHeaders)
                .contentLength(byteArrayResource.contentLength())
                .body(byteArrayResource);
        // 1.9+ 不用写 while循环  .body(new ByteArrayOutputStream(buffer));
    }
}
